package com.white.loggerserver.room

import android.content.Context
import androidx.room.*
import com.white.loggerserver.BuildConfig
import kotlin.properties.Delegates

/**
 * Created by ytf on 2020/06/08.
 * Description:
 */
@Database(entities = [Record::class, StyleConfig::class], version = 1, exportSchema = false)
abstract class MyRoomDb : RoomDatabase(){

    abstract fun recordDao() : RecordDao

    abstract fun styleConfigDao() : StyleConfigDaoDao

    companion object{

        const val DBName = "app.db"

        const val recordTableName = "test_record"

        const val styleTableName = "test_style"

        var instance : MyRoomDb by Delegates.notNull()

        fun init(context : Context){
            val builder = Room.databaseBuilder(
                context.applicationContext,
                MyRoomDb::class.java,
                DBName //数据库名称
            ).allowMainThreadQueries()
            if (!BuildConfig.DEBUG) {
                // 迁移数据库如果发生错误，将会重新创建数据库，而不是发生崩溃
                builder.fallbackToDestructiveMigration()
            }
            instance = builder.build()
        }
    }
}